<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>支付中转页</title>
    <style>
        .loading {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="loading">
        <p>正在加载支付...</p>
    </div>

    <script>
        // 获取URL参数
        function getUrlParam(name) {
            const urlParams = new URLSearchParams(window.location.search);
            return urlParams.get(name);
        }

        // 调用支付接口获取支付参数
        async function getPayParam(payParam) {
            try {
                const response = await fetch('https://nephropathy.zenzer.cn:3286/zyjv3-mp-submit-inspection-api/h5/getPayParam', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        payParam: payParam
                    })
                });
                
                const result = await response.json();
                if (result.code === 0) {
                    return result.data;
                } else {
                    throw new Error(result.msg || '获取支付参数失败');
                }
            } catch (error) {
                console.error('获取支付参数失败:', error);
                alert('获取支付参数失败: ' + error.message);
                throw error;
            }
        }

        // 调用微信支付
        function onBridgeReady(payData) {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                    appId: payData.appId,
                    timeStamp: payData.timeStamp,
                    nonceStr: payData.nonceStr,
                    package: payData.packageData,
                    signType: payData.signType,
                    paySign: payData.paySign
                },
                function(res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        alert('支付成功');
                        // 这里可以添加支付成功后的跳转逻辑
                    } else {
                        alert('支付失败或取消');
                    }
                }
            );
        }

        // 初始化支付
        async function initPayment() {
            const payParam = getUrlParam('payParam');
            if (!payParam) {
                alert('缺少支付参数');
                return;
            }

            try {
                const payData = await getPayParam(payParam);
                
                if (typeof WeixinJSBridge == "undefined") {
                    if (document.addEventListener) {
                        document.addEventListener('WeixinJSBridgeReady', () => onBridgeReady(payData), false);
                    } else if (document.attachEvent) {
                        document.attachEvent('WeixinJSBridgeReady', () => onBridgeReady(payData));
                        document.attachEvent('onWeixinJSBridgeReady', () => onBridgeReady(payData));
                    }
                } else {
                    onBridgeReady(payData);
                }
            } catch (error) {
                console.error('初始化支付失败:', error);
            }
        }

        // 页面加载完成后执行
        document.addEventListener('DOMContentLoaded', initPayment);
    </script>
</body>
</html>
